#!/usr/bin/env bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

command="gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=full.pdf"

i="$3"
while [ $i -le "$4" ]; do
    j="$1"
    while [ $j -le "$2" ]; do
        if [ $j -lt 10 ]; then
            if [ -f "0${j}hw${i}.pdf" ]; then
                command="$command 0${j}hw${i}.pdf"
            fi
            if [ -f "0${j}sol${i}.pdf" ]; then
                command="$command 0${j}sol${i}.pdf"
            fi
        else
            if [ -f "${j}hw${i}.pdf" ]; then
                command="$command ${j}hw${i}.pdf"
            fi
            if [ -f "${j}sol${i}.pdf" ]; then
                command="$command ${j}sol${i}.pdf"
            fi
        fi
        ((j++))
    done
    ((i++))
done

echo $command
eval $command
# gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${paper_name%.pdf}-merge.pdf" "$paper_name" "$supp_name"
